package com.sunny;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache1<K, V> extends LinkedHashMap<K, V>{

    private int maxCap;

    public LRUCache1(int maxCap) {
        super((int)Math.ceil(maxCap/0.75)+1, 0.75f, true);
        this.maxCap = maxCap;
    }

    @Override
    public boolean removeEldestEntry(Map.Entry<K,V> entry){
        return size() > maxCap;
    }

    public static void main(String[] args) {

        LRUCache1<String, String> cache = new LRUCache1<>(5);
        cache.put("a","s");
        cache.put("b","s");
        cache.put("c","s");
        cache.put("d","s");
        cache.put("e","s");
        cache.put("f","s");
        cache.put("b","s");
        cache.put("a","s");
        cache.put("i","s");
        cache.put("j","s");
        cache.put("a","s");

        cache.forEach((k,v)->{
            System.out.println(k+" "+v);
        });

    }

}
